<template>
    <div class="content">
		
			<!-- header部分 -->
			<header>
				<p>地址管理</p>
			</header>
			
			<!-- 地址列表部分 -->
			<ul class="addresslist">
				<li v-for="(item,index) in addresslist" :key="index">
					<div class="addresslist-left" @click="checked(index)">
						<h3>{{item.contactName}}{{item.contactSex | changeSex}} {{item.contactTel}}</h3>
                        <p>{{item.address}}</p>
					</div>
					<div class="addresslist-right">
						<i class="fa fa-edit" @click="toEdit(item.daId)"></i>
						<i class="fa fa-remove" @click="toRemove(item.daId)"></i>
					</div>
				</li>
			</ul>
			
			<!-- 新增地址部分 -->
			<div class="addbtn" @click="addaddress()">
				<i class="fa fa-plus-circle"></i>
				<p>新增收货地址</p>
			</div>
			
            <Footer></Footer>
		</div>
</template>



<script>
import Footer from '../components/Footer'
export default {
    name:'UserAddress',
    data(){
        return{
            user:'',
            addresslist:[],
            businessId: this.$route.query.businessId
        }
    },
    components:{
        Footer
    },
    created(){
        this.user = this.$getSessionStorage("user");
        //配送页地址列表初始化
        this.$axios
        .get(
          "/UserAddressController/listDeliveryAddressByUserId/"+this.user.userId
        )
        .then((response) => {
           this.addresslist = response.data.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    filters:{
        changeSex(sex){
            if(sex==1){
                return '先生'
            }else{
                return '女士'
            }
        }
    },
    methods:{
        checked(index){
            //将信息存放到localstorage
            this.$setLocalStorage(this.user.userId,this.addresslist[index]);

            //跳转回Orders页
            this.$router.push({path:'/orders',query:{businessId:this.businessId}});
        },
		addaddress(){
			this.$router.push({path:'/addaddress',query:{businessId:this.businessId}});
		},
		toEdit(daId){
			this.$router.push({path:'/editaddress',query:{businessId:this.businessId,daId:daId}});
		},
		toRemove(daId){
			let b = confirm("是否确认删除该数据？");

			if(b){
				this.$axios
				.delete(
					"/UserAddressController/removeDeliveryAddress/"+daId
				)
				.then((response) => {
					if(response.data.data>0){
						location.reload();
					}
				})
				.catch((error) => {
					console.log(error);
				});
			}
		}
    }
}
</script>


<style scoped>
/*************** 总容器 ***************/
.content{
	width: 100%;
	height: 100%;
	background-color: #F5F5F5;
}
/*************** header ***************/
.content header{
	width: 100%;
	height: 12vw; 
	background-color: #0097FF;
	display: flex;
	justify-content: space-around;
	align-items: center;
	color: #fff;
	font-size: 4.8vw;
	position: fixed;
	left: 0;
	top: 0;
	/*保证在最上层*/
	z-index: 1000; 
}

/*************** addresslist ***************/
.content .addresslist{
	width: 100%;
	margin-top: 12vw;
	background-color: #fff;
}
.content .addresslist li{
	width: 100%;
	box-sizing: border-box;
	border-bottom: solid 1px #DDD;
	padding: 3vw;
	
	display: flex;
}
.content .addresslist li .addresslist-left{
	flex:5;
	/*左边这块区域是可以点击的*/
	user-select: none;
	cursor: pointer;
}
.content .addresslist li .addresslist-left h3{
	font-size: 4.6vw;
	font-weight: 300;
	color: #666;
}
.content .addresslist li .addresslist-left p{
	font-size: 4vw;
	color: #666;
}
.content .addresslist li .addresslist-right{
	flex:1;
	font-size: 5.6vw;
	color: #999;
	cursor: pointer;
	
	display: flex;
	justify-content: space-around;
	align-items: center;
}
/*************** 新增地址部分 ***************/
.content .addbtn{
	width: 100%;
	height: 14vw;
	border-top: solid 1px #DDD;
	border-bottom: solid 1px #DDD;
	background-color: #fff;
	margin-top: 4vw;
	
	display: flex;
	justify-content: center;
	align-items: center;
	
	font-size: 4.5vw;
	color: #0097FF;
	user-select: none;
	cursor: pointer;
}
.content .addbtn p{
	margin-left: 2vw;
}

</style>